
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
  <head>
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>django.db.models.options &#8212; Django 2.2.12.dev20200304094918 documentation</title>
    <link rel="stylesheet" href="../../../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../../../_static/pygments.css" type="text/css" />
    <script type="text/javascript" id="documentation_options" data-url_root="../../../../" src="../../../../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../../../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../../../_static/doctools.js"></script>
    <script type="text/javascript" src="../../../../_static/language_data.js"></script>
    <link rel="index" title="Index" href="../../../../genindex.html" />
    <link rel="search" title="Search" href="../../../../search.html" />



 
<script type="text/javascript" src="../../../../templatebuiltins.js"></script>
<script type="text/javascript">
(function($) {
    if (!django_template_builtins) {
       // templatebuiltins.js missing, do nothing.
       return;
    }
    $(document).ready(function() {
        // Hyperlink Django template tags and filters
        var base = "../../../../ref/templates/builtins.html";
        if (base == "#") {
            // Special case for builtins.html itself
            base = "";
        }
        // Tags are keywords, class '.k'
        $("div.highlight\\-html\\+django span.k").each(function(i, elem) {
             var tagname = $(elem).text();
             if ($.inArray(tagname, django_template_builtins.ttags) != -1) {
                 var fragment = tagname.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>");
             }
        });
        // Filters are functions, class '.nf'
        $("div.highlight\\-html\\+django span.nf").each(function(i, elem) {
             var filtername = $(elem).text();
             if ($.inArray(filtername, django_template_builtins.tfilters) != -1) {
                 var fragment = filtername.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>");
             }
        });
    });
})(jQuery);</script>

  </head><body>

    <div class="document">
  <div id="custom-doc" class="yui-t6">
    <div id="hd">
      <h1><a href="../../../../index.html">Django 2.2.12.dev20200304094918 documentation</a></h1>
      <div id="global-nav">
        <a title="Home page" href="../../../../index.html">Home</a>  |
        <a title="Table of contents" href="../../../../contents.html">Table of contents</a>  |
        <a title="Global index" href="../../../../genindex.html">Index</a>  |
        <a title="Module index" href="../../../../py-modindex.html">Modules</a>
      </div>
      <div class="nav">
    <a href="../../../index.html" title="Module code" accesskey="U">up</a></div>
    </div>

    <div id="bd">
      <div id="yui-main">
        <div class="yui-b">
          <div class="yui-g" id="_modules-django-db-models-options">
            
  <h1>Source code for django.db.models.options</h1><div class="highlight"><pre>
<span></span><span class="kn">import</span> <span class="nn">copy</span>
<span class="kn">import</span> <span class="nn">inspect</span>
<span class="kn">from</span> <span class="nn">bisect</span> <span class="k">import</span> <span class="n">bisect</span>
<span class="kn">from</span> <span class="nn">collections</span> <span class="k">import</span> <span class="n">OrderedDict</span><span class="p">,</span> <span class="n">defaultdict</span>

<span class="kn">from</span> <span class="nn">django.apps</span> <span class="k">import</span> <span class="n">apps</span>
<span class="kn">from</span> <span class="nn">django.conf</span> <span class="k">import</span> <span class="n">settings</span>
<span class="kn">from</span> <span class="nn">django.core.exceptions</span> <span class="k">import</span> <span class="n">FieldDoesNotExist</span><span class="p">,</span> <span class="n">ImproperlyConfigured</span>
<span class="kn">from</span> <span class="nn">django.db</span> <span class="k">import</span> <span class="n">connections</span>
<span class="kn">from</span> <span class="nn">django.db.models</span> <span class="k">import</span> <span class="n">Manager</span>
<span class="kn">from</span> <span class="nn">django.db.models.fields</span> <span class="k">import</span> <span class="n">AutoField</span>
<span class="kn">from</span> <span class="nn">django.db.models.fields.proxy</span> <span class="k">import</span> <span class="n">OrderWrt</span>
<span class="kn">from</span> <span class="nn">django.db.models.query_utils</span> <span class="k">import</span> <span class="n">PathInfo</span>
<span class="kn">from</span> <span class="nn">django.utils.datastructures</span> <span class="k">import</span> <span class="n">ImmutableList</span><span class="p">,</span> <span class="n">OrderedSet</span>
<span class="kn">from</span> <span class="nn">django.utils.functional</span> <span class="k">import</span> <span class="n">cached_property</span>
<span class="kn">from</span> <span class="nn">django.utils.text</span> <span class="k">import</span> <span class="n">camel_case_to_spaces</span><span class="p">,</span> <span class="n">format_lazy</span>
<span class="kn">from</span> <span class="nn">django.utils.translation</span> <span class="k">import</span> <span class="n">override</span>

<span class="n">PROXY_PARENTS</span> <span class="o">=</span> <span class="nb">object</span><span class="p">()</span>

<span class="n">EMPTY_RELATION_TREE</span> <span class="o">=</span> <span class="p">()</span>

<span class="n">IMMUTABLE_WARNING</span> <span class="o">=</span> <span class="p">(</span>
    <span class="s2">&quot;The return type of &#39;</span><span class="si">%s</span><span class="s2">&#39; should never be mutated. If you want to manipulate this list &quot;</span>
    <span class="s2">&quot;for your own use, make a copy first.&quot;</span>
<span class="p">)</span>

<span class="n">DEFAULT_NAMES</span> <span class="o">=</span> <span class="p">(</span>
    <span class="s1">&#39;verbose_name&#39;</span><span class="p">,</span> <span class="s1">&#39;verbose_name_plural&#39;</span><span class="p">,</span> <span class="s1">&#39;db_table&#39;</span><span class="p">,</span> <span class="s1">&#39;ordering&#39;</span><span class="p">,</span>
    <span class="s1">&#39;unique_together&#39;</span><span class="p">,</span> <span class="s1">&#39;permissions&#39;</span><span class="p">,</span> <span class="s1">&#39;get_latest_by&#39;</span><span class="p">,</span> <span class="s1">&#39;order_with_respect_to&#39;</span><span class="p">,</span>
    <span class="s1">&#39;app_label&#39;</span><span class="p">,</span> <span class="s1">&#39;db_tablespace&#39;</span><span class="p">,</span> <span class="s1">&#39;abstract&#39;</span><span class="p">,</span> <span class="s1">&#39;managed&#39;</span><span class="p">,</span> <span class="s1">&#39;proxy&#39;</span><span class="p">,</span> <span class="s1">&#39;swappable&#39;</span><span class="p">,</span>
    <span class="s1">&#39;auto_created&#39;</span><span class="p">,</span> <span class="s1">&#39;index_together&#39;</span><span class="p">,</span> <span class="s1">&#39;apps&#39;</span><span class="p">,</span> <span class="s1">&#39;default_permissions&#39;</span><span class="p">,</span>
    <span class="s1">&#39;select_on_save&#39;</span><span class="p">,</span> <span class="s1">&#39;default_related_name&#39;</span><span class="p">,</span> <span class="s1">&#39;required_db_features&#39;</span><span class="p">,</span>
    <span class="s1">&#39;required_db_vendor&#39;</span><span class="p">,</span> <span class="s1">&#39;base_manager_name&#39;</span><span class="p">,</span> <span class="s1">&#39;default_manager_name&#39;</span><span class="p">,</span>
    <span class="s1">&#39;indexes&#39;</span><span class="p">,</span> <span class="s1">&#39;constraints&#39;</span><span class="p">,</span>
    <span class="c1"># For backwards compatibility with Django 1.11. RemovedInDjango30Warning</span>
    <span class="s1">&#39;manager_inheritance_from_future&#39;</span><span class="p">,</span>
<span class="p">)</span>


<span class="k">def</span> <span class="nf">normalize_together</span><span class="p">(</span><span class="n">option_together</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    option_together can be either a tuple of tuples, or a single</span>
<span class="sd">    tuple of two strings. Normalize it to a tuple of tuples, so that</span>
<span class="sd">    calling code can uniformly expect that.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">try</span><span class="p">:</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">option_together</span><span class="p">:</span>
            <span class="k">return</span> <span class="p">()</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">option_together</span><span class="p">,</span> <span class="p">(</span><span class="nb">tuple</span><span class="p">,</span> <span class="nb">list</span><span class="p">)):</span>
            <span class="k">raise</span> <span class="ne">TypeError</span>
        <span class="n">first_element</span> <span class="o">=</span> <span class="n">option_together</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">first_element</span><span class="p">,</span> <span class="p">(</span><span class="nb">tuple</span><span class="p">,</span> <span class="nb">list</span><span class="p">)):</span>
            <span class="n">option_together</span> <span class="o">=</span> <span class="p">(</span><span class="n">option_together</span><span class="p">,)</span>
        <span class="c1"># Normalize everything to tuples</span>
        <span class="k">return</span> <span class="nb">tuple</span><span class="p">(</span><span class="nb">tuple</span><span class="p">(</span><span class="n">ot</span><span class="p">)</span> <span class="k">for</span> <span class="n">ot</span> <span class="ow">in</span> <span class="n">option_together</span><span class="p">)</span>
    <span class="k">except</span> <span class="ne">TypeError</span><span class="p">:</span>
        <span class="c1"># If the value of option_together isn&#39;t valid, return it</span>
        <span class="c1"># verbatim; this will be picked up by the check framework later.</span>
        <span class="k">return</span> <span class="n">option_together</span>


<span class="k">def</span> <span class="nf">make_immutable_fields_list</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">data</span><span class="p">):</span>
    <span class="k">return</span> <span class="n">ImmutableList</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">warning</span><span class="o">=</span><span class="n">IMMUTABLE_WARNING</span> <span class="o">%</span> <span class="n">name</span><span class="p">)</span>


<div class="viewcode-block" id="Options"><a class="viewcode-back" href="../../../../ref/models/meta.html#django.db.models.options.Options">[docs]</a><span class="k">class</span> <span class="nc">Options</span><span class="p">:</span>
    <span class="n">FORWARD_PROPERTIES</span> <span class="o">=</span> <span class="p">{</span>
        <span class="s1">&#39;fields&#39;</span><span class="p">,</span> <span class="s1">&#39;many_to_many&#39;</span><span class="p">,</span> <span class="s1">&#39;concrete_fields&#39;</span><span class="p">,</span> <span class="s1">&#39;local_concrete_fields&#39;</span><span class="p">,</span>
        <span class="s1">&#39;_forward_fields_map&#39;</span><span class="p">,</span> <span class="s1">&#39;managers&#39;</span><span class="p">,</span> <span class="s1">&#39;managers_map&#39;</span><span class="p">,</span> <span class="s1">&#39;base_manager&#39;</span><span class="p">,</span>
        <span class="s1">&#39;default_manager&#39;</span><span class="p">,</span>
    <span class="p">}</span>
    <span class="n">REVERSE_PROPERTIES</span> <span class="o">=</span> <span class="p">{</span><span class="s1">&#39;related_objects&#39;</span><span class="p">,</span> <span class="s1">&#39;fields_map&#39;</span><span class="p">,</span> <span class="s1">&#39;_relation_tree&#39;</span><span class="p">}</span>

    <span class="n">default_apps</span> <span class="o">=</span> <span class="n">apps</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">meta</span><span class="p">,</span> <span class="n">app_label</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_get_fields_cache</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">local_fields</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">local_many_to_many</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">private_fields</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">local_managers</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">base_manager_name</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">default_manager_name</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">model_name</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">verbose_name</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">verbose_name_plural</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">db_table</span> <span class="o">=</span> <span class="s1">&#39;&#39;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">ordering</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_ordering_clash</span> <span class="o">=</span> <span class="kc">False</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">indexes</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">constraints</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">unique_together</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">index_together</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">select_on_save</span> <span class="o">=</span> <span class="kc">False</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">default_permissions</span> <span class="o">=</span> <span class="p">(</span><span class="s1">&#39;add&#39;</span><span class="p">,</span> <span class="s1">&#39;change&#39;</span><span class="p">,</span> <span class="s1">&#39;delete&#39;</span><span class="p">,</span> <span class="s1">&#39;view&#39;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">permissions</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">object_name</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">app_label</span> <span class="o">=</span> <span class="n">app_label</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">get_latest_by</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">order_with_respect_to</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">db_tablespace</span> <span class="o">=</span> <span class="n">settings</span><span class="o">.</span><span class="n">DEFAULT_TABLESPACE</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">required_db_features</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">required_db_vendor</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">meta</span> <span class="o">=</span> <span class="n">meta</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">pk</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">auto_field</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">abstract</span> <span class="o">=</span> <span class="kc">False</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">managed</span> <span class="o">=</span> <span class="kc">True</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">proxy</span> <span class="o">=</span> <span class="kc">False</span>
        <span class="c1"># For any class that is a proxy (including automatically created</span>
        <span class="c1"># classes for deferred object loading), proxy_for_model tells us</span>
        <span class="c1"># which class this model is proxying. Note that proxy_for_model</span>
        <span class="c1"># can create a chain of proxy models. For non-proxy models, the</span>
        <span class="c1"># variable is always None.</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">proxy_for_model</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="c1"># For any non-abstract class, the concrete class is the model</span>
        <span class="c1"># in the end of the proxy_for_model chain. In particular, for</span>
        <span class="c1"># concrete models, the concrete_model is always the class itself.</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">concrete_model</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">swappable</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">parents</span> <span class="o">=</span> <span class="n">OrderedDict</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">auto_created</span> <span class="o">=</span> <span class="kc">False</span>

        <span class="c1"># List of all lookups defined in ForeignKey &#39;limit_choices_to&#39; options</span>
        <span class="c1"># from *other* models. Needed for some admin checks. Internal use only.</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">related_fkey_lookups</span> <span class="o">=</span> <span class="p">[]</span>

        <span class="c1"># A custom app registry to use, if you&#39;re making a separate model set.</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">apps</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">default_apps</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">default_related_name</span> <span class="o">=</span> <span class="kc">None</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">label</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="s1">&#39;</span><span class="si">%s</span><span class="s1">.</span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">app_label</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">object_name</span><span class="p">)</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">label_lower</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="s1">&#39;</span><span class="si">%s</span><span class="s1">.</span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">app_label</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">model_name</span><span class="p">)</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">app_config</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="c1"># Don&#39;t go through get_app_config to avoid triggering imports.</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">apps</span><span class="o">.</span><span class="n">app_configs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">app_label</span><span class="p">)</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">installed</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">app_config</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span>

    <span class="k">def</span> <span class="nf">contribute_to_class</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="bp">cls</span><span class="p">,</span> <span class="n">name</span><span class="p">):</span>
        <span class="kn">from</span> <span class="nn">django.db</span> <span class="k">import</span> <span class="n">connection</span>
        <span class="kn">from</span> <span class="nn">django.db.backends.utils</span> <span class="k">import</span> <span class="n">truncate_name</span>

        <span class="bp">cls</span><span class="o">.</span><span class="n">_meta</span> <span class="o">=</span> <span class="bp">self</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">model</span> <span class="o">=</span> <span class="bp">cls</span>
        <span class="c1"># First, construct the default values for these options.</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">object_name</span> <span class="o">=</span> <span class="bp">cls</span><span class="o">.</span><span class="vm">__name__</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">model_name</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">object_name</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">verbose_name</span> <span class="o">=</span> <span class="n">camel_case_to_spaces</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">object_name</span><span class="p">)</span>

        <span class="c1"># Store the original user-defined values for each option,</span>
        <span class="c1"># for use when serializing the model definition</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">original_attrs</span> <span class="o">=</span> <span class="p">{}</span>

        <span class="c1"># Next, apply any overridden values from &#39;class Meta&#39;.</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">meta</span><span class="p">:</span>
            <span class="n">meta_attrs</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">meta</span><span class="o">.</span><span class="vm">__dict__</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>
            <span class="k">for</span> <span class="n">name</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">meta</span><span class="o">.</span><span class="vm">__dict__</span><span class="p">:</span>
                <span class="c1"># Ignore any private attributes that Django doesn&#39;t care about.</span>
                <span class="c1"># NOTE: We can&#39;t modify a dictionary&#39;s contents while looping</span>
                <span class="c1"># over it, so we loop over the *original* dictionary instead.</span>
                <span class="k">if</span> <span class="n">name</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39;_&#39;</span><span class="p">):</span>
                    <span class="k">del</span> <span class="n">meta_attrs</span><span class="p">[</span><span class="n">name</span><span class="p">]</span>
            <span class="k">for</span> <span class="n">attr_name</span> <span class="ow">in</span> <span class="n">DEFAULT_NAMES</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">attr_name</span> <span class="ow">in</span> <span class="n">meta_attrs</span><span class="p">:</span>
                    <span class="nb">setattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">attr_name</span><span class="p">,</span> <span class="n">meta_attrs</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="n">attr_name</span><span class="p">))</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">original_attrs</span><span class="p">[</span><span class="n">attr_name</span><span class="p">]</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">attr_name</span><span class="p">)</span>
                <span class="k">elif</span> <span class="nb">hasattr</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">meta</span><span class="p">,</span> <span class="n">attr_name</span><span class="p">):</span>
                    <span class="nb">setattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">attr_name</span><span class="p">,</span> <span class="nb">getattr</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">meta</span><span class="p">,</span> <span class="n">attr_name</span><span class="p">))</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">original_attrs</span><span class="p">[</span><span class="n">attr_name</span><span class="p">]</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">attr_name</span><span class="p">)</span>

            <span class="bp">self</span><span class="o">.</span><span class="n">unique_together</span> <span class="o">=</span> <span class="n">normalize_together</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">unique_together</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">index_together</span> <span class="o">=</span> <span class="n">normalize_together</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">index_together</span><span class="p">)</span>

            <span class="c1"># verbose_name_plural is a special case because it uses a &#39;s&#39;</span>
            <span class="c1"># by default.</span>
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">verbose_name_plural</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">verbose_name_plural</span> <span class="o">=</span> <span class="n">format_lazy</span><span class="p">(</span><span class="s1">&#39;</span><span class="si">{}</span><span class="s1">s&#39;</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">verbose_name</span><span class="p">)</span>

            <span class="c1"># order_with_respect_and ordering are mutually exclusive.</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_ordering_clash</span> <span class="o">=</span> <span class="nb">bool</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">ordering</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">order_with_respect_to</span><span class="p">)</span>

            <span class="c1"># Any leftover attributes must be invalid.</span>
            <span class="k">if</span> <span class="n">meta_attrs</span> <span class="o">!=</span> <span class="p">{}:</span>
                <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s2">&quot;&#39;class Meta&#39; got invalid attribute(s): </span><span class="si">%s</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="s1">&#39;,&#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">meta_attrs</span><span class="p">))</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">verbose_name_plural</span> <span class="o">=</span> <span class="n">format_lazy</span><span class="p">(</span><span class="s1">&#39;</span><span class="si">{}</span><span class="s1">s&#39;</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">verbose_name</span><span class="p">)</span>
        <span class="k">del</span> <span class="bp">self</span><span class="o">.</span><span class="n">meta</span>

        <span class="c1"># If the db_table wasn&#39;t provided, use the app_label + model_name.</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">db_table</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">db_table</span> <span class="o">=</span> <span class="s2">&quot;</span><span class="si">%s</span><span class="s2">_</span><span class="si">%s</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">app_label</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">model_name</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">db_table</span> <span class="o">=</span> <span class="n">truncate_name</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">db_table</span><span class="p">,</span> <span class="n">connection</span><span class="o">.</span><span class="n">ops</span><span class="o">.</span><span class="n">max_name_length</span><span class="p">())</span>

    <span class="k">def</span> <span class="nf">_prepare</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">model</span><span class="p">):</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">order_with_respect_to</span><span class="p">:</span>
            <span class="c1"># The app registry will not be ready at this point, so we cannot</span>
            <span class="c1"># use get_field().</span>
            <span class="n">query</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">order_with_respect_to</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">order_with_respect_to</span> <span class="o">=</span> <span class="nb">next</span><span class="p">(</span>
                    <span class="n">f</span> <span class="k">for</span> <span class="n">f</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_fields</span><span class="p">(</span><span class="n">reverse</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
                    <span class="k">if</span> <span class="n">f</span><span class="o">.</span><span class="n">name</span> <span class="o">==</span> <span class="n">query</span> <span class="ow">or</span> <span class="n">f</span><span class="o">.</span><span class="n">attname</span> <span class="o">==</span> <span class="n">query</span>
                <span class="p">)</span>
            <span class="k">except</span> <span class="ne">StopIteration</span><span class="p">:</span>
                <span class="k">raise</span> <span class="n">FieldDoesNotExist</span><span class="p">(</span><span class="s2">&quot;</span><span class="si">%s</span><span class="s2"> has no field named &#39;</span><span class="si">%s</span><span class="s2">&#39;&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">object_name</span><span class="p">,</span> <span class="n">query</span><span class="p">))</span>

            <span class="bp">self</span><span class="o">.</span><span class="n">ordering</span> <span class="o">=</span> <span class="p">(</span><span class="s1">&#39;_order&#39;</span><span class="p">,)</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="nb">any</span><span class="p">(</span><span class="nb">isinstance</span><span class="p">(</span><span class="n">field</span><span class="p">,</span> <span class="n">OrderWrt</span><span class="p">)</span> <span class="k">for</span> <span class="n">field</span> <span class="ow">in</span> <span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">local_fields</span><span class="p">):</span>
                <span class="n">model</span><span class="o">.</span><span class="n">add_to_class</span><span class="p">(</span><span class="s1">&#39;_order&#39;</span><span class="p">,</span> <span class="n">OrderWrt</span><span class="p">())</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">order_with_respect_to</span> <span class="o">=</span> <span class="kc">None</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">pk</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">parents</span><span class="p">:</span>
                <span class="c1"># Promote the first parent link in lieu of adding yet another</span>
                <span class="c1"># field.</span>
                <span class="n">field</span> <span class="o">=</span> <span class="nb">next</span><span class="p">(</span><span class="nb">iter</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">parents</span><span class="o">.</span><span class="n">values</span><span class="p">()))</span>
                <span class="c1"># Look for a local field with the same name as the</span>
                <span class="c1"># first parent link. If a local field has already been</span>
                <span class="c1"># created, use it instead of promoting the parent</span>
                <span class="n">already_created</span> <span class="o">=</span> <span class="p">[</span><span class="n">fld</span> <span class="k">for</span> <span class="n">fld</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">local_fields</span> <span class="k">if</span> <span class="n">fld</span><span class="o">.</span><span class="n">name</span> <span class="o">==</span> <span class="n">field</span><span class="o">.</span><span class="n">name</span><span class="p">]</span>
                <span class="k">if</span> <span class="n">already_created</span><span class="p">:</span>
                    <span class="n">field</span> <span class="o">=</span> <span class="n">already_created</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
                <span class="n">field</span><span class="o">.</span><span class="n">primary_key</span> <span class="o">=</span> <span class="kc">True</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">setup_pk</span><span class="p">(</span><span class="n">field</span><span class="p">)</span>
                <span class="k">if</span> <span class="ow">not</span> <span class="n">field</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">parent_link</span><span class="p">:</span>
                    <span class="k">raise</span> <span class="n">ImproperlyConfigured</span><span class="p">(</span>
                        <span class="s1">&#39;Add parent_link=True to </span><span class="si">%s</span><span class="s1">.&#39;</span> <span class="o">%</span> <span class="n">field</span><span class="p">,</span>
                    <span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">auto</span> <span class="o">=</span> <span class="n">AutoField</span><span class="p">(</span><span class="n">verbose_name</span><span class="o">=</span><span class="s1">&#39;ID&#39;</span><span class="p">,</span> <span class="n">primary_key</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">auto_created</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
                <span class="n">model</span><span class="o">.</span><span class="n">add_to_class</span><span class="p">(</span><span class="s1">&#39;id&#39;</span><span class="p">,</span> <span class="n">auto</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">add_manager</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">manager</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">local_managers</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">manager</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_expire_cache</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">add_field</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">field</span><span class="p">,</span> <span class="n">private</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
        <span class="c1"># Insert the given field in the order in which it was created, using</span>
        <span class="c1"># the &quot;creation_counter&quot; attribute of the field.</span>
        <span class="c1"># Move many-to-many related fields from self.fields into</span>
        <span class="c1"># self.many_to_many.</span>
        <span class="k">if</span> <span class="n">private</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">private_fields</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">field</span><span class="p">)</span>
        <span class="k">elif</span> <span class="n">field</span><span class="o">.</span><span class="n">is_relation</span> <span class="ow">and</span> <span class="n">field</span><span class="o">.</span><span class="n">many_to_many</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">local_many_to_many</span><span class="o">.</span><span class="n">insert</span><span class="p">(</span><span class="n">bisect</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">local_many_to_many</span><span class="p">,</span> <span class="n">field</span><span class="p">),</span> <span class="n">field</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">local_fields</span><span class="o">.</span><span class="n">insert</span><span class="p">(</span><span class="n">bisect</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">local_fields</span><span class="p">,</span> <span class="n">field</span><span class="p">),</span> <span class="n">field</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">setup_pk</span><span class="p">(</span><span class="n">field</span><span class="p">)</span>

        <span class="c1"># If the field being added is a relation to another known field,</span>
        <span class="c1"># expire the cache on this field and the forward cache on the field</span>
        <span class="c1"># being referenced, because there will be new relationships in the</span>
        <span class="c1"># cache. Otherwise, expire the cache of references *to* this field.</span>
        <span class="c1"># The mechanism for getting at the related model is slightly odd -</span>
        <span class="c1"># ideally, we&#39;d just ask for field.related_model. However, related_model</span>
        <span class="c1"># is a cached property, and all the models haven&#39;t been loaded yet, so</span>
        <span class="c1"># we need to make sure we don&#39;t cache a string reference.</span>
        <span class="k">if</span> <span class="n">field</span><span class="o">.</span><span class="n">is_relation</span> <span class="ow">and</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">field</span><span class="o">.</span><span class="n">remote_field</span><span class="p">,</span> <span class="s1">&#39;model&#39;</span><span class="p">)</span> <span class="ow">and</span> <span class="n">field</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">model</span><span class="p">:</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="n">field</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">_expire_cache</span><span class="p">(</span><span class="n">forward</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
            <span class="k">except</span> <span class="ne">AttributeError</span><span class="p">:</span>
                <span class="k">pass</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_expire_cache</span><span class="p">()</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_expire_cache</span><span class="p">(</span><span class="n">reverse</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">setup_pk</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">field</span><span class="p">):</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">pk</span> <span class="ow">and</span> <span class="n">field</span><span class="o">.</span><span class="n">primary_key</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">pk</span> <span class="o">=</span> <span class="n">field</span>
            <span class="n">field</span><span class="o">.</span><span class="n">serialize</span> <span class="o">=</span> <span class="kc">False</span>

    <span class="k">def</span> <span class="nf">setup_proxy</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">target</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Do the internal setup so that the current model is a proxy for</span>
<span class="sd">        &quot;target&quot;.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">pk</span> <span class="o">=</span> <span class="n">target</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">pk</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">proxy_for_model</span> <span class="o">=</span> <span class="n">target</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">db_table</span> <span class="o">=</span> <span class="n">target</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">db_table</span>

    <span class="k">def</span> <span class="nf">__repr__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="s1">&#39;&lt;Options for </span><span class="si">%s</span><span class="s1">&gt;&#39;</span> <span class="o">%</span> <span class="bp">self</span><span class="o">.</span><span class="n">object_name</span>

    <span class="k">def</span> <span class="nf">__str__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="s2">&quot;</span><span class="si">%s</span><span class="s2">.</span><span class="si">%s</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">app_label</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">model_name</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">can_migrate</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">connection</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Return True if the model can/should be migrated on the `connection`.</span>
<span class="sd">        `connection` can be either a real connection or a connection alias.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">proxy</span> <span class="ow">or</span> <span class="bp">self</span><span class="o">.</span><span class="n">swapped</span> <span class="ow">or</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">managed</span><span class="p">:</span>
            <span class="k">return</span> <span class="kc">False</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">connection</span><span class="p">,</span> <span class="nb">str</span><span class="p">):</span>
            <span class="n">connection</span> <span class="o">=</span> <span class="n">connections</span><span class="p">[</span><span class="n">connection</span><span class="p">]</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">required_db_vendor</span><span class="p">:</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">required_db_vendor</span> <span class="o">==</span> <span class="n">connection</span><span class="o">.</span><span class="n">vendor</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">required_db_features</span><span class="p">:</span>
            <span class="k">return</span> <span class="nb">all</span><span class="p">(</span><span class="nb">getattr</span><span class="p">(</span><span class="n">connection</span><span class="o">.</span><span class="n">features</span><span class="p">,</span> <span class="n">feat</span><span class="p">,</span> <span class="kc">False</span><span class="p">)</span>
                       <span class="k">for</span> <span class="n">feat</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">required_db_features</span><span class="p">)</span>
        <span class="k">return</span> <span class="kc">True</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">verbose_name_raw</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Return the untranslated verbose name.&quot;&quot;&quot;</span>
        <span class="k">with</span> <span class="n">override</span><span class="p">(</span><span class="kc">None</span><span class="p">):</span>
            <span class="k">return</span> <span class="nb">str</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">verbose_name</span><span class="p">)</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">swapped</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Has this model been swapped out for another? If so, return the model</span>
<span class="sd">        name of the replacement; otherwise, return None.</span>

<span class="sd">        For historical reasons, model name lookups using get_model() are</span>
<span class="sd">        case insensitive, so we make sure we are case insensitive here.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">swappable</span><span class="p">:</span>
            <span class="n">swapped_for</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">settings</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">swappable</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">swapped_for</span><span class="p">:</span>
                <span class="k">try</span><span class="p">:</span>
                    <span class="n">swapped_label</span><span class="p">,</span> <span class="n">swapped_object</span> <span class="o">=</span> <span class="n">swapped_for</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">&#39;.&#39;</span><span class="p">)</span>
                <span class="k">except</span> <span class="ne">ValueError</span><span class="p">:</span>
                    <span class="c1"># setting not in the format app_label.model_name</span>
                    <span class="c1"># raising ImproperlyConfigured here causes problems with</span>
                    <span class="c1"># test cleanup code - instead it is raised in get_user_model</span>
                    <span class="c1"># or as part of validation.</span>
                    <span class="k">return</span> <span class="n">swapped_for</span>

                <span class="k">if</span> <span class="s1">&#39;</span><span class="si">%s</span><span class="s1">.</span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">swapped_label</span><span class="p">,</span> <span class="n">swapped_object</span><span class="o">.</span><span class="n">lower</span><span class="p">())</span> <span class="o">!=</span> <span class="bp">self</span><span class="o">.</span><span class="n">label_lower</span><span class="p">:</span>
                    <span class="k">return</span> <span class="n">swapped_for</span>
        <span class="k">return</span> <span class="kc">None</span>

    <span class="nd">@cached_property</span>
    <span class="k">def</span> <span class="nf">managers</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">managers</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">seen_managers</span> <span class="o">=</span> <span class="nb">set</span><span class="p">()</span>
        <span class="n">bases</span> <span class="o">=</span> <span class="p">(</span><span class="n">b</span> <span class="k">for</span> <span class="n">b</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">mro</span><span class="p">()</span> <span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">b</span><span class="p">,</span> <span class="s1">&#39;_meta&#39;</span><span class="p">))</span>
        <span class="k">for</span> <span class="n">depth</span><span class="p">,</span> <span class="n">base</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">bases</span><span class="p">):</span>
            <span class="k">for</span> <span class="n">manager</span> <span class="ow">in</span> <span class="n">base</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">local_managers</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">manager</span><span class="o">.</span><span class="n">name</span> <span class="ow">in</span> <span class="n">seen_managers</span><span class="p">:</span>
                    <span class="k">continue</span>

                <span class="n">manager</span> <span class="o">=</span> <span class="n">copy</span><span class="o">.</span><span class="n">copy</span><span class="p">(</span><span class="n">manager</span><span class="p">)</span>
                <span class="n">manager</span><span class="o">.</span><span class="n">model</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span>
                <span class="n">seen_managers</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">manager</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>
                <span class="n">managers</span><span class="o">.</span><span class="n">append</span><span class="p">((</span><span class="n">depth</span><span class="p">,</span> <span class="n">manager</span><span class="o">.</span><span class="n">creation_counter</span><span class="p">,</span> <span class="n">manager</span><span class="p">))</span>

        <span class="k">return</span> <span class="n">make_immutable_fields_list</span><span class="p">(</span>
            <span class="s2">&quot;managers&quot;</span><span class="p">,</span>
            <span class="p">(</span><span class="n">m</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span> <span class="k">for</span> <span class="n">m</span> <span class="ow">in</span> <span class="nb">sorted</span><span class="p">(</span><span class="n">managers</span><span class="p">)),</span>
        <span class="p">)</span>

    <span class="nd">@cached_property</span>
    <span class="k">def</span> <span class="nf">managers_map</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="p">{</span><span class="n">manager</span><span class="o">.</span><span class="n">name</span><span class="p">:</span> <span class="n">manager</span> <span class="k">for</span> <span class="n">manager</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">managers</span><span class="p">}</span>

    <span class="nd">@cached_property</span>
    <span class="k">def</span> <span class="nf">base_manager</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">base_manager_name</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">base_manager_name</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">base_manager_name</span><span class="p">:</span>
            <span class="c1"># Get the first parent&#39;s base_manager_name if there&#39;s one.</span>
            <span class="k">for</span> <span class="n">parent</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">mro</span><span class="p">()[</span><span class="mi">1</span><span class="p">:]:</span>
                <span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">parent</span><span class="p">,</span> <span class="s1">&#39;_meta&#39;</span><span class="p">):</span>
                    <span class="k">if</span> <span class="n">parent</span><span class="o">.</span><span class="n">_base_manager</span><span class="o">.</span><span class="n">name</span> <span class="o">!=</span> <span class="s1">&#39;_base_manager&#39;</span><span class="p">:</span>
                        <span class="n">base_manager_name</span> <span class="o">=</span> <span class="n">parent</span><span class="o">.</span><span class="n">_base_manager</span><span class="o">.</span><span class="n">name</span>
                    <span class="k">break</span>

        <span class="k">if</span> <span class="n">base_manager_name</span><span class="p">:</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">managers_map</span><span class="p">[</span><span class="n">base_manager_name</span><span class="p">]</span>
            <span class="k">except</span> <span class="ne">KeyError</span><span class="p">:</span>
                <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span>
                    <span class="s2">&quot;</span><span class="si">%s</span><span class="s2"> has no manager named </span><span class="si">%r</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="p">(</span>
                        <span class="bp">self</span><span class="o">.</span><span class="n">object_name</span><span class="p">,</span>
                        <span class="n">base_manager_name</span><span class="p">,</span>
                    <span class="p">)</span>
                <span class="p">)</span>

        <span class="n">manager</span> <span class="o">=</span> <span class="n">Manager</span><span class="p">()</span>
        <span class="n">manager</span><span class="o">.</span><span class="n">name</span> <span class="o">=</span> <span class="s1">&#39;_base_manager&#39;</span>
        <span class="n">manager</span><span class="o">.</span><span class="n">model</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span>
        <span class="n">manager</span><span class="o">.</span><span class="n">auto_created</span> <span class="o">=</span> <span class="kc">True</span>
        <span class="k">return</span> <span class="n">manager</span>

    <span class="nd">@cached_property</span>
    <span class="k">def</span> <span class="nf">default_manager</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">default_manager_name</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">default_manager_name</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">default_manager_name</span> <span class="ow">and</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">local_managers</span><span class="p">:</span>
            <span class="c1"># Get the first parent&#39;s default_manager_name if there&#39;s one.</span>
            <span class="k">for</span> <span class="n">parent</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">mro</span><span class="p">()[</span><span class="mi">1</span><span class="p">:]:</span>
                <span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">parent</span><span class="p">,</span> <span class="s1">&#39;_meta&#39;</span><span class="p">):</span>
                    <span class="n">default_manager_name</span> <span class="o">=</span> <span class="n">parent</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">default_manager_name</span>
                    <span class="k">break</span>

        <span class="k">if</span> <span class="n">default_manager_name</span><span class="p">:</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">managers_map</span><span class="p">[</span><span class="n">default_manager_name</span><span class="p">]</span>
            <span class="k">except</span> <span class="ne">KeyError</span><span class="p">:</span>
                <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span>
                    <span class="s2">&quot;</span><span class="si">%s</span><span class="s2"> has no manager named </span><span class="si">%r</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="p">(</span>
                        <span class="bp">self</span><span class="o">.</span><span class="n">object_name</span><span class="p">,</span>
                        <span class="n">default_manager_name</span><span class="p">,</span>
                    <span class="p">)</span>
                <span class="p">)</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">managers</span><span class="p">:</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">managers</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>

    <span class="nd">@cached_property</span>
    <span class="k">def</span> <span class="nf">fields</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Return a list of all forward fields on the model and its parents,</span>
<span class="sd">        excluding ManyToManyFields.</span>

<span class="sd">        Private API intended only to be used by Django itself; get_fields()</span>
<span class="sd">        combined with filtering of field properties is the public API for</span>
<span class="sd">        obtaining this field list.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="c1"># For legacy reasons, the fields property should only contain forward</span>
        <span class="c1"># fields that are not private or with a m2m cardinality. Therefore we</span>
        <span class="c1"># pass these three filters as filters to the generator.</span>
        <span class="c1"># The third lambda is a longwinded way of checking f.related_model - we don&#39;t</span>
        <span class="c1"># use that property directly because related_model is a cached property,</span>
        <span class="c1"># and all the models may not have been loaded yet; we don&#39;t want to cache</span>
        <span class="c1"># the string reference to the related_model.</span>
        <span class="k">def</span> <span class="nf">is_not_an_m2m_field</span><span class="p">(</span><span class="n">f</span><span class="p">):</span>
            <span class="k">return</span> <span class="ow">not</span> <span class="p">(</span><span class="n">f</span><span class="o">.</span><span class="n">is_relation</span> <span class="ow">and</span> <span class="n">f</span><span class="o">.</span><span class="n">many_to_many</span><span class="p">)</span>

        <span class="k">def</span> <span class="nf">is_not_a_generic_relation</span><span class="p">(</span><span class="n">f</span><span class="p">):</span>
            <span class="k">return</span> <span class="ow">not</span> <span class="p">(</span><span class="n">f</span><span class="o">.</span><span class="n">is_relation</span> <span class="ow">and</span> <span class="n">f</span><span class="o">.</span><span class="n">one_to_many</span><span class="p">)</span>

        <span class="k">def</span> <span class="nf">is_not_a_generic_foreign_key</span><span class="p">(</span><span class="n">f</span><span class="p">):</span>
            <span class="k">return</span> <span class="ow">not</span> <span class="p">(</span>
                <span class="n">f</span><span class="o">.</span><span class="n">is_relation</span> <span class="ow">and</span> <span class="n">f</span><span class="o">.</span><span class="n">many_to_one</span> <span class="ow">and</span> <span class="ow">not</span> <span class="p">(</span><span class="nb">hasattr</span><span class="p">(</span><span class="n">f</span><span class="o">.</span><span class="n">remote_field</span><span class="p">,</span> <span class="s1">&#39;model&#39;</span><span class="p">)</span> <span class="ow">and</span> <span class="n">f</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">model</span><span class="p">)</span>
            <span class="p">)</span>

        <span class="k">return</span> <span class="n">make_immutable_fields_list</span><span class="p">(</span>
            <span class="s2">&quot;fields&quot;</span><span class="p">,</span>
            <span class="p">(</span><span class="n">f</span> <span class="k">for</span> <span class="n">f</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_fields</span><span class="p">(</span><span class="n">reverse</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
             <span class="k">if</span> <span class="n">is_not_an_m2m_field</span><span class="p">(</span><span class="n">f</span><span class="p">)</span> <span class="ow">and</span> <span class="n">is_not_a_generic_relation</span><span class="p">(</span><span class="n">f</span><span class="p">)</span> <span class="ow">and</span> <span class="n">is_not_a_generic_foreign_key</span><span class="p">(</span><span class="n">f</span><span class="p">))</span>
        <span class="p">)</span>

    <span class="nd">@cached_property</span>
    <span class="k">def</span> <span class="nf">concrete_fields</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Return a list of all concrete fields on the model and its parents.</span>

<span class="sd">        Private API intended only to be used by Django itself; get_fields()</span>
<span class="sd">        combined with filtering of field properties is the public API for</span>
<span class="sd">        obtaining this field list.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="n">make_immutable_fields_list</span><span class="p">(</span>
            <span class="s2">&quot;concrete_fields&quot;</span><span class="p">,</span> <span class="p">(</span><span class="n">f</span> <span class="k">for</span> <span class="n">f</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">fields</span> <span class="k">if</span> <span class="n">f</span><span class="o">.</span><span class="n">concrete</span><span class="p">)</span>
        <span class="p">)</span>

    <span class="nd">@cached_property</span>
    <span class="k">def</span> <span class="nf">local_concrete_fields</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Return a list of all concrete fields on the model.</span>

<span class="sd">        Private API intended only to be used by Django itself; get_fields()</span>
<span class="sd">        combined with filtering of field properties is the public API for</span>
<span class="sd">        obtaining this field list.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="n">make_immutable_fields_list</span><span class="p">(</span>
            <span class="s2">&quot;local_concrete_fields&quot;</span><span class="p">,</span> <span class="p">(</span><span class="n">f</span> <span class="k">for</span> <span class="n">f</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">local_fields</span> <span class="k">if</span> <span class="n">f</span><span class="o">.</span><span class="n">concrete</span><span class="p">)</span>
        <span class="p">)</span>

    <span class="nd">@cached_property</span>
    <span class="k">def</span> <span class="nf">many_to_many</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Return a list of all many to many fields on the model and its parents.</span>

<span class="sd">        Private API intended only to be used by Django itself; get_fields()</span>
<span class="sd">        combined with filtering of field properties is the public API for</span>
<span class="sd">        obtaining this list.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="n">make_immutable_fields_list</span><span class="p">(</span>
            <span class="s2">&quot;many_to_many&quot;</span><span class="p">,</span>
            <span class="p">(</span><span class="n">f</span> <span class="k">for</span> <span class="n">f</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_fields</span><span class="p">(</span><span class="n">reverse</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span> <span class="k">if</span> <span class="n">f</span><span class="o">.</span><span class="n">is_relation</span> <span class="ow">and</span> <span class="n">f</span><span class="o">.</span><span class="n">many_to_many</span><span class="p">)</span>
        <span class="p">)</span>

    <span class="nd">@cached_property</span>
    <span class="k">def</span> <span class="nf">related_objects</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Return all related objects pointing to the current model. The related</span>
<span class="sd">        objects can come from a one-to-one, one-to-many, or many-to-many field</span>
<span class="sd">        relation type.</span>

<span class="sd">        Private API intended only to be used by Django itself; get_fields()</span>
<span class="sd">        combined with filtering of field properties is the public API for</span>
<span class="sd">        obtaining this field list.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">all_related_fields</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_fields</span><span class="p">(</span><span class="n">forward</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">reverse</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">include_hidden</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">make_immutable_fields_list</span><span class="p">(</span>
            <span class="s2">&quot;related_objects&quot;</span><span class="p">,</span>
            <span class="p">(</span><span class="n">obj</span> <span class="k">for</span> <span class="n">obj</span> <span class="ow">in</span> <span class="n">all_related_fields</span> <span class="k">if</span> <span class="ow">not</span> <span class="n">obj</span><span class="o">.</span><span class="n">hidden</span> <span class="ow">or</span> <span class="n">obj</span><span class="o">.</span><span class="n">field</span><span class="o">.</span><span class="n">many_to_many</span><span class="p">)</span>
        <span class="p">)</span>

    <span class="nd">@cached_property</span>
    <span class="k">def</span> <span class="nf">_forward_fields_map</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">res</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="n">fields</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_fields</span><span class="p">(</span><span class="n">reverse</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
        <span class="k">for</span> <span class="n">field</span> <span class="ow">in</span> <span class="n">fields</span><span class="p">:</span>
            <span class="n">res</span><span class="p">[</span><span class="n">field</span><span class="o">.</span><span class="n">name</span><span class="p">]</span> <span class="o">=</span> <span class="n">field</span>
            <span class="c1"># Due to the way Django&#39;s internals work, get_field() should also</span>
            <span class="c1"># be able to fetch a field by attname. In the case of a concrete</span>
            <span class="c1"># field with relation, includes the *_id name too</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="n">res</span><span class="p">[</span><span class="n">field</span><span class="o">.</span><span class="n">attname</span><span class="p">]</span> <span class="o">=</span> <span class="n">field</span>
            <span class="k">except</span> <span class="ne">AttributeError</span><span class="p">:</span>
                <span class="k">pass</span>
        <span class="k">return</span> <span class="n">res</span>

    <span class="nd">@cached_property</span>
    <span class="k">def</span> <span class="nf">fields_map</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">res</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="n">fields</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_fields</span><span class="p">(</span><span class="n">forward</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">include_hidden</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
        <span class="k">for</span> <span class="n">field</span> <span class="ow">in</span> <span class="n">fields</span><span class="p">:</span>
            <span class="n">res</span><span class="p">[</span><span class="n">field</span><span class="o">.</span><span class="n">name</span><span class="p">]</span> <span class="o">=</span> <span class="n">field</span>
            <span class="c1"># Due to the way Django&#39;s internals work, get_field() should also</span>
            <span class="c1"># be able to fetch a field by attname. In the case of a concrete</span>
            <span class="c1"># field with relation, includes the *_id name too</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="n">res</span><span class="p">[</span><span class="n">field</span><span class="o">.</span><span class="n">attname</span><span class="p">]</span> <span class="o">=</span> <span class="n">field</span>
            <span class="k">except</span> <span class="ne">AttributeError</span><span class="p">:</span>
                <span class="k">pass</span>
        <span class="k">return</span> <span class="n">res</span>

<div class="viewcode-block" id="Options.get_field"><a class="viewcode-back" href="../../../../ref/models/meta.html#django.db.models.options.Options.get_field">[docs]</a>    <span class="k">def</span> <span class="nf">get_field</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">field_name</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Return a field instance given the name of a forward or reverse field.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="c1"># In order to avoid premature loading of the relation tree</span>
            <span class="c1"># (expensive) we prefer checking if the field is a forward field.</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_forward_fields_map</span><span class="p">[</span><span class="n">field_name</span><span class="p">]</span>
        <span class="k">except</span> <span class="ne">KeyError</span><span class="p">:</span>
            <span class="c1"># If the app registry is not ready, reverse fields are</span>
            <span class="c1"># unavailable, therefore we throw a FieldDoesNotExist exception.</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">apps</span><span class="o">.</span><span class="n">models_ready</span><span class="p">:</span>
                <span class="k">raise</span> <span class="n">FieldDoesNotExist</span><span class="p">(</span>
                    <span class="s2">&quot;</span><span class="si">%s</span><span class="s2"> has no field named &#39;</span><span class="si">%s</span><span class="s2">&#39;. The app cache isn&#39;t ready yet, &quot;</span>
                    <span class="s2">&quot;so if this is an auto-created related field, it won&#39;t &quot;</span>
                    <span class="s2">&quot;be available yet.&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">object_name</span><span class="p">,</span> <span class="n">field_name</span><span class="p">)</span>
                <span class="p">)</span>

        <span class="k">try</span><span class="p">:</span>
            <span class="c1"># Retrieve field instance by name from cached or just-computed</span>
            <span class="c1"># field map.</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">fields_map</span><span class="p">[</span><span class="n">field_name</span><span class="p">]</span>
        <span class="k">except</span> <span class="ne">KeyError</span><span class="p">:</span>
            <span class="k">raise</span> <span class="n">FieldDoesNotExist</span><span class="p">(</span><span class="s2">&quot;</span><span class="si">%s</span><span class="s2"> has no field named &#39;</span><span class="si">%s</span><span class="s2">&#39;&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">object_name</span><span class="p">,</span> <span class="n">field_name</span><span class="p">))</span></div>

    <span class="k">def</span> <span class="nf">get_base_chain</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">model</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Return a list of parent classes leading to `model` (ordered from</span>
<span class="sd">        closest to most distant ancestor). This has to handle the case where</span>
<span class="sd">        `model` is a grandparent or even more distant relation.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">parents</span><span class="p">:</span>
            <span class="k">return</span> <span class="p">[]</span>
        <span class="k">if</span> <span class="n">model</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">parents</span><span class="p">:</span>
            <span class="k">return</span> <span class="p">[</span><span class="n">model</span><span class="p">]</span>
        <span class="k">for</span> <span class="n">parent</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">parents</span><span class="p">:</span>
            <span class="n">res</span> <span class="o">=</span> <span class="n">parent</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">get_base_chain</span><span class="p">(</span><span class="n">model</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">res</span><span class="p">:</span>
                <span class="n">res</span><span class="o">.</span><span class="n">insert</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">parent</span><span class="p">)</span>
                <span class="k">return</span> <span class="n">res</span>
        <span class="k">return</span> <span class="p">[]</span>

    <span class="k">def</span> <span class="nf">get_parent_list</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Return all the ancestors of this model as a list ordered by MRO.</span>
<span class="sd">        Useful for determining if something is an ancestor, regardless of lineage.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">result</span> <span class="o">=</span> <span class="n">OrderedSet</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">parents</span><span class="p">)</span>
        <span class="k">for</span> <span class="n">parent</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">parents</span><span class="p">:</span>
            <span class="k">for</span> <span class="n">ancestor</span> <span class="ow">in</span> <span class="n">parent</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">get_parent_list</span><span class="p">():</span>
                <span class="n">result</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">ancestor</span><span class="p">)</span>
        <span class="k">return</span> <span class="nb">list</span><span class="p">(</span><span class="n">result</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">get_ancestor_link</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">ancestor</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Return the field on the current model which points to the given</span>
<span class="sd">        &quot;ancestor&quot;. This is possible an indirect link (a pointer to a parent</span>
<span class="sd">        model, which points, eventually, to the ancestor). Used when</span>
<span class="sd">        constructing table joins for model inheritance.</span>

<span class="sd">        Return None if the model isn&#39;t an ancestor of this one.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="n">ancestor</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">parents</span><span class="p">:</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">parents</span><span class="p">[</span><span class="n">ancestor</span><span class="p">]</span>
        <span class="k">for</span> <span class="n">parent</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">parents</span><span class="p">:</span>
            <span class="c1"># Tries to get a link field from the immediate parent</span>
            <span class="n">parent_link</span> <span class="o">=</span> <span class="n">parent</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">get_ancestor_link</span><span class="p">(</span><span class="n">ancestor</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">parent_link</span><span class="p">:</span>
                <span class="c1"># In case of a proxied model, the first link</span>
                <span class="c1"># of the chain to the ancestor is that parent</span>
                <span class="c1"># links</span>
                <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">parents</span><span class="p">[</span><span class="n">parent</span><span class="p">]</span> <span class="ow">or</span> <span class="n">parent_link</span>

    <span class="k">def</span> <span class="nf">get_path_to_parent</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">parent</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Return a list of PathInfos containing the path from the current</span>
<span class="sd">        model to the parent model, or an empty list if parent is not a</span>
<span class="sd">        parent of the current model.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span> <span class="ow">is</span> <span class="n">parent</span><span class="p">:</span>
            <span class="k">return</span> <span class="p">[]</span>
        <span class="c1"># Skip the chain of proxy to the concrete proxied model.</span>
        <span class="n">proxied_model</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">concrete_model</span>
        <span class="n">path</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">opts</span> <span class="o">=</span> <span class="bp">self</span>
        <span class="k">for</span> <span class="n">int_model</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_base_chain</span><span class="p">(</span><span class="n">parent</span><span class="p">):</span>
            <span class="k">if</span> <span class="n">int_model</span> <span class="ow">is</span> <span class="n">proxied_model</span><span class="p">:</span>
                <span class="n">opts</span> <span class="o">=</span> <span class="n">int_model</span><span class="o">.</span><span class="n">_meta</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">final_field</span> <span class="o">=</span> <span class="n">opts</span><span class="o">.</span><span class="n">parents</span><span class="p">[</span><span class="n">int_model</span><span class="p">]</span>
                <span class="n">targets</span> <span class="o">=</span> <span class="p">(</span><span class="n">final_field</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">get_related_field</span><span class="p">(),)</span>
                <span class="n">opts</span> <span class="o">=</span> <span class="n">int_model</span><span class="o">.</span><span class="n">_meta</span>
                <span class="n">path</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">PathInfo</span><span class="p">(</span>
                    <span class="n">from_opts</span><span class="o">=</span><span class="n">final_field</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="p">,</span>
                    <span class="n">to_opts</span><span class="o">=</span><span class="n">opts</span><span class="p">,</span>
                    <span class="n">target_fields</span><span class="o">=</span><span class="n">targets</span><span class="p">,</span>
                    <span class="n">join_field</span><span class="o">=</span><span class="n">final_field</span><span class="p">,</span>
                    <span class="n">m2m</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
                    <span class="n">direct</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span>
                    <span class="n">filtered_relation</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
                <span class="p">))</span>
        <span class="k">return</span> <span class="n">path</span>

    <span class="k">def</span> <span class="nf">get_path_from_parent</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">parent</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Return a list of PathInfos containing the path from the parent</span>
<span class="sd">        model to the current model, or an empty list if parent is not a</span>
<span class="sd">        parent of the current model.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span> <span class="ow">is</span> <span class="n">parent</span><span class="p">:</span>
            <span class="k">return</span> <span class="p">[]</span>
        <span class="n">model</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">concrete_model</span>
        <span class="c1"># Get a reversed base chain including both the current and parent</span>
        <span class="c1"># models.</span>
        <span class="n">chain</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">get_base_chain</span><span class="p">(</span><span class="n">parent</span><span class="p">)</span>
        <span class="n">chain</span><span class="o">.</span><span class="n">reverse</span><span class="p">()</span>
        <span class="n">chain</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">model</span><span class="p">)</span>
        <span class="c1"># Construct a list of the PathInfos between models in chain.</span>
        <span class="n">path</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">ancestor</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">chain</span><span class="p">[:</span><span class="o">-</span><span class="mi">1</span><span class="p">]):</span>
            <span class="n">child</span> <span class="o">=</span> <span class="n">chain</span><span class="p">[</span><span class="n">i</span> <span class="o">+</span> <span class="mi">1</span><span class="p">]</span>
            <span class="n">link</span> <span class="o">=</span> <span class="n">child</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">get_ancestor_link</span><span class="p">(</span><span class="n">ancestor</span><span class="p">)</span>
            <span class="n">path</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="n">link</span><span class="o">.</span><span class="n">get_reverse_path_info</span><span class="p">())</span>
        <span class="k">return</span> <span class="n">path</span>

    <span class="k">def</span> <span class="nf">_populate_directed_relation_graph</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        This method is used by each model to find its reverse objects. As this</span>
<span class="sd">        method is very expensive and is accessed frequently (it looks up every</span>
<span class="sd">        field in a model, in every app), it is computed on first access and then</span>
<span class="sd">        is set as a property on every model.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">related_objects_graph</span> <span class="o">=</span> <span class="n">defaultdict</span><span class="p">(</span><span class="nb">list</span><span class="p">)</span>

        <span class="n">all_models</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">apps</span><span class="o">.</span><span class="n">get_models</span><span class="p">(</span><span class="n">include_auto_created</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
        <span class="k">for</span> <span class="n">model</span> <span class="ow">in</span> <span class="n">all_models</span><span class="p">:</span>
            <span class="n">opts</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">_meta</span>
            <span class="c1"># Abstract model&#39;s fields are copied to child models, hence we will</span>
            <span class="c1"># see the fields from the child models.</span>
            <span class="k">if</span> <span class="n">opts</span><span class="o">.</span><span class="n">abstract</span><span class="p">:</span>
                <span class="k">continue</span>
            <span class="n">fields_with_relations</span> <span class="o">=</span> <span class="p">(</span>
                <span class="n">f</span> <span class="k">for</span> <span class="n">f</span> <span class="ow">in</span> <span class="n">opts</span><span class="o">.</span><span class="n">_get_fields</span><span class="p">(</span><span class="n">reverse</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">include_parents</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
                <span class="k">if</span> <span class="n">f</span><span class="o">.</span><span class="n">is_relation</span> <span class="ow">and</span> <span class="n">f</span><span class="o">.</span><span class="n">related_model</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span>
            <span class="p">)</span>
            <span class="k">for</span> <span class="n">f</span> <span class="ow">in</span> <span class="n">fields_with_relations</span><span class="p">:</span>
                <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">f</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">model</span><span class="p">,</span> <span class="nb">str</span><span class="p">):</span>
                    <span class="n">related_objects_graph</span><span class="p">[</span><span class="n">f</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">concrete_model</span><span class="o">.</span><span class="n">_meta</span><span class="p">]</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">f</span><span class="p">)</span>

        <span class="k">for</span> <span class="n">model</span> <span class="ow">in</span> <span class="n">all_models</span><span class="p">:</span>
            <span class="c1"># Set the relation_tree using the internal __dict__. In this way</span>
            <span class="c1"># we avoid calling the cached property. In attribute lookup,</span>
            <span class="c1"># __dict__ takes precedence over a data descriptor (such as</span>
            <span class="c1"># @cached_property). This means that the _meta._relation_tree is</span>
            <span class="c1"># only called if related_objects is not in __dict__.</span>
            <span class="n">related_objects</span> <span class="o">=</span> <span class="n">related_objects_graph</span><span class="p">[</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">concrete_model</span><span class="o">.</span><span class="n">_meta</span><span class="p">]</span>
            <span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="vm">__dict__</span><span class="p">[</span><span class="s1">&#39;_relation_tree&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">related_objects</span>
        <span class="c1"># It seems it is possible that self is not in all_models, so guard</span>
        <span class="c1"># against that with default for get().</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="vm">__dict__</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;_relation_tree&#39;</span><span class="p">,</span> <span class="n">EMPTY_RELATION_TREE</span><span class="p">)</span>

    <span class="nd">@cached_property</span>
    <span class="k">def</span> <span class="nf">_relation_tree</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_populate_directed_relation_graph</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">_expire_cache</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">forward</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">reverse</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span>
        <span class="c1"># This method is usually called by apps.cache_clear(), when the</span>
        <span class="c1"># registry is finalized, or when a new field is added.</span>
        <span class="k">if</span> <span class="n">forward</span><span class="p">:</span>
            <span class="k">for</span> <span class="n">cache_key</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">FORWARD_PROPERTIES</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">cache_key</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="vm">__dict__</span><span class="p">:</span>
                    <span class="nb">delattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">cache_key</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">reverse</span> <span class="ow">and</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">abstract</span><span class="p">:</span>
            <span class="k">for</span> <span class="n">cache_key</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">REVERSE_PROPERTIES</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">cache_key</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="vm">__dict__</span><span class="p">:</span>
                    <span class="nb">delattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">cache_key</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_get_fields_cache</span> <span class="o">=</span> <span class="p">{}</span>

<div class="viewcode-block" id="Options.get_fields"><a class="viewcode-back" href="../../../../ref/models/meta.html#django.db.models.options.Options.get_fields">[docs]</a>    <span class="k">def</span> <span class="nf">get_fields</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">include_parents</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">include_hidden</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Return a list of fields associated to the model. By default, include</span>
<span class="sd">        forward and reverse fields, fields derived from inheritance, but not</span>
<span class="sd">        hidden fields. The returned fields can be changed using the parameters:</span>

<span class="sd">        - include_parents: include fields derived from inheritance</span>
<span class="sd">        - include_hidden:  include fields that have a related_name that</span>
<span class="sd">                           starts with a &quot;+&quot;</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="n">include_parents</span> <span class="ow">is</span> <span class="kc">False</span><span class="p">:</span>
            <span class="n">include_parents</span> <span class="o">=</span> <span class="n">PROXY_PARENTS</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_fields</span><span class="p">(</span><span class="n">include_parents</span><span class="o">=</span><span class="n">include_parents</span><span class="p">,</span> <span class="n">include_hidden</span><span class="o">=</span><span class="n">include_hidden</span><span class="p">)</span></div>

    <span class="k">def</span> <span class="nf">_get_fields</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">forward</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">reverse</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">include_parents</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">include_hidden</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
                    <span class="n">seen_models</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Internal helper function to return fields of the model.</span>
<span class="sd">        * If forward=True, then fields defined on this model are returned.</span>
<span class="sd">        * If reverse=True, then relations pointing to this model are returned.</span>
<span class="sd">        * If include_hidden=True, then fields with is_hidden=True are returned.</span>
<span class="sd">        * The include_parents argument toggles if fields from parent models</span>
<span class="sd">          should be included. It has three values: True, False, and</span>
<span class="sd">          PROXY_PARENTS. When set to PROXY_PARENTS, the call will return all</span>
<span class="sd">          fields defined for the current model or any of its parents in the</span>
<span class="sd">          parent chain to the model&#39;s concrete model.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="n">include_parents</span> <span class="ow">not</span> <span class="ow">in</span> <span class="p">(</span><span class="kc">True</span><span class="p">,</span> <span class="kc">False</span><span class="p">,</span> <span class="n">PROXY_PARENTS</span><span class="p">):</span>
            <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s2">&quot;Invalid argument for include_parents: </span><span class="si">%s</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">include_parents</span><span class="p">,))</span>
        <span class="c1"># This helper function is used to allow recursion in ``get_fields()``</span>
        <span class="c1"># implementation and to provide a fast way for Django&#39;s internals to</span>
        <span class="c1"># access specific subsets of fields.</span>

        <span class="c1"># We must keep track of which models we have already seen. Otherwise we</span>
        <span class="c1"># could include the same field multiple times from different models.</span>
        <span class="n">topmost_call</span> <span class="o">=</span> <span class="n">seen_models</span> <span class="ow">is</span> <span class="kc">None</span>
        <span class="k">if</span> <span class="n">topmost_call</span><span class="p">:</span>
            <span class="n">seen_models</span> <span class="o">=</span> <span class="nb">set</span><span class="p">()</span>
        <span class="n">seen_models</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="p">)</span>

        <span class="c1"># Creates a cache key composed of all arguments</span>
        <span class="n">cache_key</span> <span class="o">=</span> <span class="p">(</span><span class="n">forward</span><span class="p">,</span> <span class="n">reverse</span><span class="p">,</span> <span class="n">include_parents</span><span class="p">,</span> <span class="n">include_hidden</span><span class="p">,</span> <span class="n">topmost_call</span><span class="p">)</span>

        <span class="k">try</span><span class="p">:</span>
            <span class="c1"># In order to avoid list manipulation. Always return a shallow copy</span>
            <span class="c1"># of the results.</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_fields_cache</span><span class="p">[</span><span class="n">cache_key</span><span class="p">]</span>
        <span class="k">except</span> <span class="ne">KeyError</span><span class="p">:</span>
            <span class="k">pass</span>

        <span class="n">fields</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="c1"># Recursively call _get_fields() on each parent, with the same</span>
        <span class="c1"># options provided in this call.</span>
        <span class="k">if</span> <span class="n">include_parents</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">False</span><span class="p">:</span>
            <span class="k">for</span> <span class="n">parent</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">parents</span><span class="p">:</span>
                <span class="c1"># In diamond inheritance it is possible that we see the same</span>
                <span class="c1"># model from two different routes. In that case, avoid adding</span>
                <span class="c1"># fields from the same parent again.</span>
                <span class="k">if</span> <span class="n">parent</span> <span class="ow">in</span> <span class="n">seen_models</span><span class="p">:</span>
                    <span class="k">continue</span>
                <span class="k">if</span> <span class="p">(</span><span class="n">parent</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">concrete_model</span> <span class="o">!=</span> <span class="bp">self</span><span class="o">.</span><span class="n">concrete_model</span> <span class="ow">and</span>
                        <span class="n">include_parents</span> <span class="o">==</span> <span class="n">PROXY_PARENTS</span><span class="p">):</span>
                    <span class="k">continue</span>
                <span class="k">for</span> <span class="n">obj</span> <span class="ow">in</span> <span class="n">parent</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">_get_fields</span><span class="p">(</span>
                        <span class="n">forward</span><span class="o">=</span><span class="n">forward</span><span class="p">,</span> <span class="n">reverse</span><span class="o">=</span><span class="n">reverse</span><span class="p">,</span> <span class="n">include_parents</span><span class="o">=</span><span class="n">include_parents</span><span class="p">,</span>
                        <span class="n">include_hidden</span><span class="o">=</span><span class="n">include_hidden</span><span class="p">,</span> <span class="n">seen_models</span><span class="o">=</span><span class="n">seen_models</span><span class="p">):</span>
                    <span class="k">if</span> <span class="ow">not</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">obj</span><span class="p">,</span> <span class="s1">&#39;parent_link&#39;</span><span class="p">,</span> <span class="kc">False</span><span class="p">)</span> <span class="ow">or</span> <span class="n">obj</span><span class="o">.</span><span class="n">model</span> <span class="o">==</span> <span class="bp">self</span><span class="o">.</span><span class="n">concrete_model</span><span class="p">:</span>
                        <span class="n">fields</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">obj</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">reverse</span> <span class="ow">and</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">proxy</span><span class="p">:</span>
            <span class="c1"># Tree is computed once and cached until the app cache is expired.</span>
            <span class="c1"># It is composed of a list of fields pointing to the current model</span>
            <span class="c1"># from other models.</span>
            <span class="n">all_fields</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_relation_tree</span>
            <span class="k">for</span> <span class="n">field</span> <span class="ow">in</span> <span class="n">all_fields</span><span class="p">:</span>
                <span class="c1"># If hidden fields should be included or the relation is not</span>
                <span class="c1"># intentionally hidden, add to the fields dict.</span>
                <span class="k">if</span> <span class="n">include_hidden</span> <span class="ow">or</span> <span class="ow">not</span> <span class="n">field</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">hidden</span><span class="p">:</span>
                    <span class="n">fields</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">field</span><span class="o">.</span><span class="n">remote_field</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">forward</span><span class="p">:</span>
            <span class="n">fields</span> <span class="o">+=</span> <span class="bp">self</span><span class="o">.</span><span class="n">local_fields</span>
            <span class="n">fields</span> <span class="o">+=</span> <span class="bp">self</span><span class="o">.</span><span class="n">local_many_to_many</span>
            <span class="c1"># Private fields are recopied to each child model, and they get a</span>
            <span class="c1"># different model as field.model in each child. Hence we have to</span>
            <span class="c1"># add the private fields separately from the topmost call. If we</span>
            <span class="c1"># did this recursively similar to local_fields, we would get field</span>
            <span class="c1"># instances with field.model != self.model.</span>
            <span class="k">if</span> <span class="n">topmost_call</span><span class="p">:</span>
                <span class="n">fields</span> <span class="o">+=</span> <span class="bp">self</span><span class="o">.</span><span class="n">private_fields</span>

        <span class="c1"># In order to avoid list manipulation. Always</span>
        <span class="c1"># return a shallow copy of the results</span>
        <span class="n">fields</span> <span class="o">=</span> <span class="n">make_immutable_fields_list</span><span class="p">(</span><span class="s2">&quot;get_fields()&quot;</span><span class="p">,</span> <span class="n">fields</span><span class="p">)</span>

        <span class="c1"># Store result into cache for later access</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_get_fields_cache</span><span class="p">[</span><span class="n">cache_key</span><span class="p">]</span> <span class="o">=</span> <span class="n">fields</span>
        <span class="k">return</span> <span class="n">fields</span>

    <span class="nd">@cached_property</span>
    <span class="k">def</span> <span class="nf">_property_names</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Return a set of the names of the properties defined on the model.&quot;&quot;&quot;</span>
        <span class="n">names</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">name</span> <span class="ow">in</span> <span class="nb">dir</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="p">):</span>
            <span class="n">attr</span> <span class="o">=</span> <span class="n">inspect</span><span class="o">.</span><span class="n">getattr_static</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="p">,</span> <span class="n">name</span><span class="p">)</span>
            <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">attr</span><span class="p">,</span> <span class="nb">property</span><span class="p">):</span>
                <span class="n">names</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">name</span><span class="p">)</span>
        <span class="k">return</span> <span class="nb">frozenset</span><span class="p">(</span><span class="n">names</span><span class="p">)</span></div>
</pre></div>

          </div>
        </div>
      </div>
      
        
          <div class="yui-b" id="sidebar">
            
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
<div id="searchbox" style="display: none" role="search">
  <h3>Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="../../../../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    </div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
              <h3>Last update:</h3>
              <p class="topless">Mar 04, 2020</p>
          </div>
        
      
    </div>

    <div id="ft">
      <div class="nav">
    <a href="../../../index.html" title="Module code" accesskey="U">up</a></div>
    </div>
  </div>

      <div class="clearer"></div>
    </div>
  </body>
</html>